<?php
class Hopdong_AdminHdController extends Zendvn_Controller_Action{
	
	protected $_arrParam;
	protected $_currentController;
	protected $_actionMain;
	
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 10,
									'currentPage' => 1
									);
	protected $_namespace;
	
	protected $_page = '';
	protected $_id_kh = '';
	
	public function init(){
		$this->_arrParam = $this->_request->getParams();
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index';
		$this->_controllerMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller']; //Duong dan cua Controller chinh
	
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//Lay thong tin so phan tu tren mot trang
		if(isset($this->_arrParam['limitPage'])){
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif(!empty($ssFilter->limitPage)){
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		if(isset($this->_arrParam['page'])){
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}
		
		if(!empty($this->_arrParam['id_kh'])){
			$this->_page .= '/id_kh/' . $this->_arrParam['id_kh'];
			$this->_id_kh = '/id_kh/' . $this->_arrParam['id_kh'];
		}
		
		$this->_arrParam['paginator'] = $this->_paginator;
		
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
			$ssFilter->tungay	 	= '';
			$ssFilter->denngay	 	= '';
			$ssFilter->col 			= 'hd.ngayky';
			$ssFilter->order 		= 'DESC';
			$ssFilter->id_khoahoc	= 0;
			$ssFilter->id_lophoc	= 0;
			$ssFilter->blocks		= 0;
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['tungay'] 		= $ssFilter->tungay;
		$this->_arrParam['ssFilter']['denngay'] 	= $ssFilter->denngay;
		$this->_arrParam['ssFilter']['col'] 		= $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 		= $ssFilter->order;
		$this->_arrParam['ssFilter']['id_khoahoc'] 	= $ssFilter->id_khoahoc;
		$this->_arrParam['ssFilter']['id_lophoc'] 	= $ssFilter->id_lophoc;
		$this->_arrParam['ssFilter']['blocks'] 		= $ssFilter->blocks;
		$this->_arrParam['ssFilter']['id_phongban'] = $ssFilter->id_phongban;
		$this->_arrParam['ssFilter']['id_nhanvien'] = $ssFilter->id_nhanvien;
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'customer');
	}

	public function indexAction(){
		//var_dump($this->_arrParam);die;
		$this->view->Title = 'Hợp đồng :: Danh sách';
		$this->view->headTitle($this->view->Title, true);

		$tblHopDong = new Hopdong_Model_Hd();
		$this->view->Items = $tblHopDong->listItem($this->_arrParam, array('task'=>'admin-list'));

		$tblAjax = new Khachhang_Model_Ajax();
		$this->view->slbKhoahoc = $tblAjax->itemInSelectbox($this->_arrParam, array('task' => 'get-khoahoc'));
		$this->view->slbLopHoc = $tblAjax->itemInSelectbox(array('id_khoahoc' => $this->_arrParam['ssFilter']['id_khoahoc']), array('task' => 'get-lophoc'));
		
		$tblAjax1 = new Hopdong_Model_Ajax();
		$this->view->slbPhongban = $tblAjax1->itemInSelectbox($this->_arrParam, array('task' => 'get-phongban'));
		$this->view->slbNhanvien = $tblAjax1->itemInSelectbox(array('id_phongban' => $this->_arrParam['ssFilter']['id_phongban']), array('task' => 'get-nhanvien'));
		

		$totalItem = $tblHopDong->countItem($this->_arrParam, array('task'=>'admin-list'));
		
		$paginator = new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	
	public function listinfoAction(){
	
		$this->view->Title = 'Xem danh sách hợp đồng nhân viên';
		$this->view->headTitle($this->view->Title, true);
		//echo $this->_arrParam['id_nhanvien'];die;
		$tblhd = new Hopdong_Model_Hd();
		$this->view->Items = $tblhd->listinfo($this->_arrParam, array('task'=>'admin-list'));
	
		
		$tblAjax = new Khachhang_Model_Ajax();
		$this->view->slbKhoahoc = $tblAjax->itemInSelectbox($this->_arrParam, array('task' => 'get-khoahoc'));
		$this->view->slbLopHoc = $tblAjax->itemInSelectbox(array('id_khoahoc' => $this->_arrParam['ssFilter']['id_khoahoc']), array('task' => 'get-lophoc'));
		
		$tblAjax1 = new Hopdong_Model_Ajax();
		$this->view->slbPhongban = $tblAjax1->itemInSelectbox($this->_arrParam, array('task' => 'get-phongban'));
		$this->view->slbNhanvien = $tblAjax1->itemInSelectbox(array('id_phongban' => $this->_arrParam['ssFilter']['id_phongban']), array('task' => 'get-nhanvien'));
		
		
		
	
		$this->view->User = $tblhd->getItem(array('id' => $this->_arrParam['id']), array('task'=>'info-nhanvien'));
	
		$totalItem = $tblhd->countItem($this->_arrParam, array('task'=>'admin-listhopdong'));
			
		$paginator = new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		 
		
		if ($this->_arrParam['type'] == 'search') {
			if ($this->_arrParam['key'] == 1) {
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
				$ssFilter->tungay = trim($this->_arrParam['tungay']);
				$ssFilter->denngay = trim($this->_arrParam['denngay']);
			}else{
				$ssFilter->keywords = '';
				$ssFilter->tungay	 = '';
				$ssFilter->denngay	 = '';
			}
		}
		
		
		
		if($this->_arrParam['type'] == 'id_khoahoc'){
			$ssFilter->id_khoahoc = $this->_arrParam['id_khoahoc'];
			$ssFilter->id_lophoc = 0;
		}
		
		if ($this->_arrParam['type'] == 'id_phongban') {
			$ssFilter->id_phongban = $this->_arrParam['id_phongban'];
			$ssFilter->id_nhanvien = 0;
		}
		
		if ($this->_arrParam['type'] == 'id_nhanvien') {
			$ssFilter->id_nhanvien = $this->_arrParam['id_nhanvien'];
		}
		
		
		if($this->_arrParam['type'] == 'id_lophoc'){
			$ssFilter->id_lophoc = $this->_arrParam['id_lophoc'];
		}
	
		if($this->_arrParam['type'] == 'order'){
			$ssFilter->col = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
		
		if($this->_arrParam['type'] == 'blocks'){
			$ssFilter->blocks = $this->_arrParam['blocks'];
		}
	
		$this->_redirect($this->_actionMain . $this->_id_kh);
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function addAction() {
		$this->view->Title = 'Hợp đồng :: Thêm';
		$this->view->headTitle($this->view->Title, true);

		$tblHopDong = new Hopdong_Model_Hopdong();
		
		$tblKhachhang = new Khachhang_Model_Item();
		$this->view->Khachhang = $tblKhachhang->getItem($this->_arrParam, array('task' => 'admin-info'));
		$this->_arrParam['khachhang'] = $this->view->Khachhang; 
		
		if($this->_request->isPost()){
			$validator = new Hopdong_Form_ValidateItem($this->_arrParam, array());
			if($validator->isError() == true) {
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			} else {
				$arrParam = $validator->getData();

				$lastID = $tblHopDong->saveItem($this->_arrParam, array('task' => 'admin-add'));
				if(empty($this->_arrParam['type'])){
					$this->_redirect($this->_actionMain . '/save/ok' . $this->_page);
				}else{
					if($this->_arrParam['type'] == 'apply'){
						$this->_redirect($this->_controllerMain . '/edit/save/ok/id/' . $lastID . $this->_page);
					}
					if($this->_arrParam['type'] == 'new'){
						$this->_redirect($this->_controllerMain . '/add/save/ok' . $this->_page);
					}
				}
			}
		}
	}
	
	public function infoAction(){
		$this->view->Title = 'Hợp đồng :: Xem thông tin';
		$this->view->headTitle($this->view->Title, true);
		$tblHopDong = new Hopdong_Model_Hopdong();
		$this->view->Item = $tblHopDong->getItem($this->_arrParam,array('task'=>'admin-info'));
		
		$tblUser = new Default_Model_Users();
		$this->view->createdBy = $tblUser->getItem(array('id' => $this->view->Item['created_by']),array('task'=>'admin-info'));
		$this->view->modifiedBy = $tblUser->getItem(array('id' => $this->view->Item['modified_by']),array('task'=>'admin-info'));
		
	}
	
	public function editAction(){
		$this->view->Title = 'Hợp đồng :: Sửa';
		$this->view->headTitle($this->view->Title, true);
		
		$tblHopDong = new Hopdong_Model_Hopdong();
		$this->view->Item = $tblHopDong->getItem($this->_arrParam, array('task'=>'admin-edit'));
		
		$tblKhachhang = new Khachhang_Model_Item();
		$this->view->Khachhang = $tblKhachhang->getItem(array('id_kh' => $this->view->Item['id_kh']), array('task' => 'admin-info'));
		$this->_arrParam['khachhang'] = $this->view->Khachhang; 
		
		if($this->_request->isPost()){
			$validator = new Hopdong_Form_ValidateItem($this->_arrParam, array());
			if($validator->isError() == true) {
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			} else {
				$arrParam = $validator->getData();

				$lastID = $tblHopDong->saveItem($this->_arrParam, array('task' => 'admin-edit'));
				if(empty($this->_arrParam['type'])){
					$this->_redirect($this->_actionMain . '/save/ok' . $this->_page);
				}else{
					if($this->_arrParam['type'] == 'apply'){
						$this->_redirect($this->_controllerMain . '/edit/save/ok/id/' . $lastID . $this->_page);
					}
					if($this->_arrParam['type'] == 'new'){
						$this->_redirect($this->_controllerMain . '/add/save/ok' . $this->_page);
					}
				}
			}
		}
	}
	
	public function deleteAction(){
		$this->view->Title = 'Hợp đồng :: Xóa';
		$this->view->headTitle($this->view->Title, true);
		if($this->_request->isPost()){
			if(($this->_arrParam['type'] == 'multi-delete') && ($this->_arrParam['task'] == 'ok')){
				$tblHopDong = new Hopdong_Model_Hopdong();
				$tblHopDong->deleteItem($this->_arrParam, array('task'=>'admin-delete-muti'));
				$this->_redirect($this->_actionMain . $this->_page);
			}else
				if(!empty($this->_arrParam['id'])){
				$tblHopDong = new Hopdong_Model_Hopdong();
				$tblHopDong->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
				$this->_redirect($this->_actionMain . $this->_page);
			}
		}
	}
	
	public function statusAction(){
		$tblHopDong = new Hopdong_Model_Hopdong();
		$tblHopDong->changeStatus($this->_arrParam);
		$this->_redirect($this->_actionMain . $this->_page);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function sortAction(){
		if($this->_request->isPost()){
			$tblHopDong = new Hopdong_Model_Hopdong();
			$tblHopDong->sortItem($this->_arrParam, array('task'=>'admin-sort'));
			$this->_redirect($this->_actionMain . $this->_page);
		}
		$this->_helper->viewRenderer->setNoRender();
	}

}



